package com.dc.project_java.test.lazy;

import com.dc.base.network.NetworkApi;
import com.dc.base.network.RequestCallback;
import com.dc.base.utils.RxJavaUtil;
import com.dc.project_java.test.TestApiService;
import com.dc.project_java.test.utils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LazyRepository {

    public Disposable getJueJin(String category, RequestCallback<JueJinBean> callback) {
        TestApiService service = NetworkApi.createService(TestApiService.class, TestApiService.JUE_JIN_BASE_URL);
        JSONObject obj = new JSONObject();
        try {
            obj.put("sort_type", 200);
            obj.put("cate_id", category);
            obj.put("limit", 20);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj.toString());
        return service.jueJin(body)
                .compose(RxJavaUtil.observableToMain())
                .subscribe(callback::onSuccess,
                        throwable -> callback.onFailure(Constant.FAIL_CODE, throwable.getMessage()));
    }
}
